<script setup>
import { ref, reactive, onMounted } from 'vue'
import { RouterLink, RouterView, routerKey } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus';
import {useRouter} from 'vue-router';
import { default as http, getImageUrl } from '@/utils/request.js';
const active = ref(3);
const router = useRouter();
const onClickLeft = () => router.push("/personal");

// Alt + Shift +F 整理代码格式
// 申请退货列表
const returnOrderList = reactive([

])

// 获取退货订单列表  
const getReturnOrderList = () => {
  // console.log("------------" + user.value);
  http.get("/user/info").then((response) => {
    console.log("获取个人信息= ", response);
    console.log("id:", response.data.data.userName);
    http.get("/user/returnorder/" + response.data.data.userName).then((res) => {
      returnOrderList.splice(0); //todo 需要将原有的元素清空  
      res.data.data.list.forEach(order => {
        returnOrderList.push(order);
      });
      console.log('获取售后订单列表: ', res.data.data.list);
      console.log('afterSendList:', returnOrderList);
      // resolve(); // 当获取待收货列表成功后，返回一个解析成功的Promise  
    }).catch(err => {
      reject(err); // 当请求失败时，返回一个请求失败的Promise  
    });

  }).catch(() => {
  });
};


// // 打开订单

// const router = useRouter();
const toReturnOrderInfo = function (reorderId) {
    console.log("退货订单ID:", reorderId);
    // 跳转至歌单详情页；请求地址：/songlist/1
    router.push({path: "/returnorderinfo/" + reorderId +"?name=zhangsan"});
}


// 取消申请
const deleteReturn = (reorderId) => {
    // 二次确认
    http.delete("/user/returnorder/"+reorderId)
        .then((res) => {
            console.log('!!!!!!!!!!!!!!!!!返回值：', res.data);
            const { code, message } = res.data;
            console.log("=========code",code);
            if (200 === code) {
                showNotify({ type: 'success', message: '取消成功' });
                setTimeout(() => {
                    router.push("/returnorder"); //使用路径导航
                }, 1000);
            } else {
                showNotify({ type: 'danger', message });
            }
        }).catch((err) => {
            console.log('取消失败：', err);
        });
}

onMounted(() => {

  getReturnOrderList();
});



</script>


<template>
          <van-col span="24" class="nav_bar">
            <van-nav-bar title="售后" left-text="返回" :fixed="true" left-arrow @click-left="onClickLeft" />
          </van-col>
       
      <van-cell-group>
        <van-tabs v-model:active="active" style="margin-top: 25px;">
          <van-tab title="待付款" to="/beforepay">售后</van-tab>
          <van-tab title="待发货" to="/backorder"></van-tab>
          <van-tab title="待收货" to="/aftersend"></van-tab>
          <van-tab title="售 后"></van-tab>
        </van-tabs>
      </van-cell-group>
  <div style="color: #c8d175; margin-top: 10px;">
    
    <van-cell-group inset>  
        <van-card v-for="re in returnOrderList" :key="re.orderNumber" :num="re.amount" :price="re.orderPrice"
          :desc="re.details" :title="re.sellName" :thumb="getImageUrl(re.picture) "
          style="margin-top: 10px; background-color:  #FFFFE0;" @click="toReturnOrderInfo(re.reorderId)">
          <template #tags>
            <van-tag plain type="primary" style="flex-grow: 1; margin-right: 10px;">{{ re.goodsName }}</van-tag>
          </template>
          <template #footer style="margin-left: 0%;">
            <van-space size="1rem">
              <van-button size="mini" @click.stop="deleteReturn(re.reorderId)">取消申请</van-button>
            </van-space>
          </template>
        </van-card>
    </van-cell-group>
  </div>

  <RouterView />
</template>

<style scoped>
header {
  position: fixed;
  top: 0;
  width: 100%;
  z-index: 9999;
  line-height: 1.5;
  max-height: 100vh;
}


header {
  line-height: 1.5;
  max-height: 100vh;
}


nav {
  width: 100%;
  font-size: 12px;
  text-align: center;
  margin-top: 2rem;
}

nav a.router-link-exact-active {
  color: var(--color-text);
}

nav a.router-link-exact-active:hover {
  background-color: transparent;
}

nav a {
  display: inline-block;
  padding: 0 1rem;
  border-left: 1px solid var(--color-border);
}

nav a:first-of-type {
  border: 0;
}

@media (min-width: 1024px) {
  header {
    display: flex;
    place-items: center;
    padding-right: calc(var(--section-gap) / 2);
  }


  header .wrapper {
    display: flex;
    place-items: flex-start;
    flex-wrap: wrap;
  }

  nav {
    text-align: left;
    margin-left: -1rem;
    font-size: 1rem;
    padding: 1rem 0;
    margin-top: 1rem;
  }
}

.van-field {
  /* 调整每一行边距 */
  padding: 16px 15px;
}

.van-nav-bar {
  color: #d4e09f;
  width: 100%;
}

.full-width-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 10px;
  /* 根据需要调整 */
}
.nav_bar{
    position:relative;
    top:-32px;
    /* left:-32px;
    right: -32px; */
    margin:2px -32px
}
</style>
